package com.jrx.anytxn.param.utils;

import com.jrx.anytxn.common.uitl.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class ReadCsvUtil {

    private static final Logger logger = LoggerFactory.getLogger(ReadCsvUtil.class);

    private static final String FIX="\uFEFF";

    /**
     * 获取csv文件内容
     * @return 对象list
     */
    public static List<Map<String,Object>> getResource(byte[] bate) throws IOException {
        List<Map<String,Object>> allString = new ArrayList();
        Map<String,Object> callLogInfo ;
        List<String> list = new ArrayList();
        // 获取文件内容
        list = getSource(bate);
        // 获取文件表头
        List<String> title = Arrays.asList(list.get(0).split(","));
        String customerId = title.get(0).trim();
        String organizationId = title.get(1).trim();
        String limitId = title.get(2).trim();
        String templateId = title.get(3).trim();
        String limitPerm = title.get(4).trim();
        String channel = title.get(5).trim();
        String dataSource = title.get(6).trim();

        if(customerId.startsWith(FIX)){
            customerId = customerId.replace(FIX, "");
        }
        callLogInfo = new HashMap();
        callLogInfo.put("customerId",customerId);
        callLogInfo.put("organizationId",organizationId);
        callLogInfo.put("limitId",limitId);
        callLogInfo.put("templateId",templateId);
        callLogInfo.put("limitPerm",limitPerm);
        callLogInfo.put("channel",channel);
        callLogInfo.put("dataSource",dataSource);
        allString.add(callLogInfo);

        list.remove(0);

        for(int i = 0; i<list.size();i++){
            List<String> content = Arrays.asList(list.get(i).split(","));
            //去掉空白行
            if(StringUtils.isNotBlank(content.get(0))){
                callLogInfo = new HashMap();
                callLogInfo.put("customerId",content.get(0));
                callLogInfo.put("organizationId",content.get(1));
                callLogInfo.put("limitId",content.get(2));
                callLogInfo.put("templateId",content.get(3));
                callLogInfo.put("limitPerm",content.get(4));
                callLogInfo.put("channel",content.get(5));
                callLogInfo.put("dataSource",content.get(6));
                allString.add(callLogInfo);
            }
        }
        return  allString;
    }

    /**
     * 读文件数据
     */
    public static List<String> getSource(byte[] bate) throws IOException {
        BufferedReader br = null;
        ByteArrayInputStream fis=null;
        InputStreamReader isr = null;
        try {
            fis = new ByteArrayInputStream(bate);
            isr = new InputStreamReader(fis,"UTF-8");
            br = new BufferedReader(isr);
        } catch (Exception e) {
            logger.error("读取文件失败",e);
        }
        String line;
        String everyLine ;
        List<String> allString = new ArrayList<>();
        try {
            while ((line = br.readLine()) != null){
                everyLine = line;
                allString.add(everyLine);
            }
        } catch (IOException e) {
            logger.error("读取文件失败",e);
        }finally {
            if(fis != null){
                fis.close();
            }
            if(isr != null){
                isr.close();
            }
        }
        return allString;
    }
}
